{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Refactoring the feature engineering steps\n",
    "\n",
    "[Feature Engineering for Time Series Forecasting](https://www.trainindata.com/p/feature-engineering-for-forecasting)\n",
    "\n",
    "In Section 2, we learned that we can extract a lot of features from a time series. We used pandas to create most features. Then, we used those features to forecast CO concentration for the next hour. \n",
    "\n",
    "In this notebook, we will use the open-source library Feature-engine to line the feature extraction steps within a Scikit-learn pipeline.\n",
    "\n",
    "**In this notebook we bring forward the feature creation steps that we implemented in the second notebook in Section 2.**\n",
    "\n",
    "We will create the following features from the hourly CO concentration:\n",
    "\n",
    "- Date and time features\n",
    "- Lag features\n",
    "- Window features\n",
    "- Cyclical features\n",
    "- Remove missing data\n",
    "\n",
    "\n",
    "## Data\n",
    "\n",
    "We will work with the Air Quality Dataset from the [UCI Machine Learning Repository](https://archive.ics.uci.edu/ml/datasets/Air+Quality).\n",
    "\n",
    "For instructions on how to download, prepare, and store the dataset, refer to notebook number 3, in the folder \"01-Datasets\" from this repo."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "\n",
    "from feature_engine.creation import CyclicalFeatures\n",
    "from feature_engine.datetime import DatetimeFeatures\n",
    "from feature_engine.imputation import DropMissingData\n",
    "from feature_engine.selection import DropFeatures\n",
    "from feature_engine.timeseries.forecasting import (\n",
    "    LagFeatures,\n",
    "    WindowFeatures,\n",
    ")\n",
    "\n",
    "from sklearn.pipeline import Pipeline"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Load data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Same function we saw in section 2.\n",
    "\n",
    "def load_data():\n",
    "\n",
    "    # Data lives here.\n",
    "    filename = \"../datasets/AirQualityUCI_ready.csv\"\n",
    "\n",
    "    # Load data: only the time variable and CO.\n",
    "    data = pd.read_csv(\n",
    "        filename,\n",
    "        usecols=[\"Date_Time\", \"CO_sensor\", \"RH\"],\n",
    "        parse_dates=[\"Date_Time\"],\n",
    "        index_col=[\"Date_Time\"],\n",
    "    )\n",
    "\n",
    "    # Sanity: sort index.\n",
    "    data.sort_index(inplace=True)\n",
    "\n",
    "    # Reduce data span.\n",
    "    data = data[\"2004-04-01\":\"2005-04-30\"]\n",
    "\n",
    "    # Remove outliers\n",
    "    data = data.loc[(data[\"CO_sensor\"] > 0)]\n",
    "\n",
    "    return data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>CO_sensor</th>\n",
       "      <th>RH</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Date_Time</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2004-04-04 00:00:00</th>\n",
       "      <td>1224.0</td>\n",
       "      <td>56.5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2004-04-04 01:00:00</th>\n",
       "      <td>1215.0</td>\n",
       "      <td>59.2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2004-04-04 02:00:00</th>\n",
       "      <td>1115.0</td>\n",
       "      <td>62.4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2004-04-04 03:00:00</th>\n",
       "      <td>1124.0</td>\n",
       "      <td>65.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2004-04-04 04:00:00</th>\n",
       "      <td>1028.0</td>\n",
       "      <td>65.3</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                     CO_sensor    RH\n",
       "Date_Time                           \n",
       "2004-04-04 00:00:00     1224.0  56.5\n",
       "2004-04-04 01:00:00     1215.0  59.2\n",
       "2004-04-04 02:00:00     1115.0  62.4\n",
       "2004-04-04 03:00:00     1124.0  65.0\n",
       "2004-04-04 04:00:00     1028.0  65.3"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Load data.\n",
    "\n",
    "data = load_data()\n",
    "\n",
    "data.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Datetime features\n",
    "\n",
    "We can extract date and time features automatically utilizing Feature-engine.\n",
    "\n",
    "[DatetimeFeatures](https://feature-engine.readthedocs.io/en/latest/api_doc/datetime/DatetimeFeatures.html)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>CO_sensor</th>\n",
       "      <th>RH</th>\n",
       "      <th>month</th>\n",
       "      <th>week</th>\n",
       "      <th>day_of_week</th>\n",
       "      <th>day_of_month</th>\n",
       "      <th>hour</th>\n",
       "      <th>weekend</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Date_Time</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2004-04-04 00:00:00</th>\n",
       "      <td>1224.0</td>\n",
       "      <td>56.5</td>\n",
       "      <td>4</td>\n",
       "      <td>14</td>\n",
       "      <td>6</td>\n",
       "      <td>4</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2004-04-04 01:00:00</th>\n",
       "      <td>1215.0</td>\n",
       "      <td>59.2</td>\n",
       "      <td>4</td>\n",
       "      <td>14</td>\n",
       "      <td>6</td>\n",
       "      <td>4</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2004-04-04 02:00:00</th>\n",
       "      <td>1115.0</td>\n",
       "      <td>62.4</td>\n",
       "      <td>4</td>\n",
       "      <td>14</td>\n",
       "      <td>6</td>\n",
       "      <td>4</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2004-04-04 03:00:00</th>\n",
       "      <td>1124.0</td>\n",
       "      <td>65.0</td>\n",
       "      <td>4</td>\n",
       "      <td>14</td>\n",
       "      <td>6</td>\n",
       "      <td>4</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2004-04-04 04:00:00</th>\n",
       "      <td>1028.0</td>\n",
       "      <td>65.3</td>\n",
       "      <td>4</td>\n",
       "      <td>14</td>\n",
       "      <td>6</td>\n",
       "      <td>4</td>\n",
       "      <td>4</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                     CO_sensor    RH  month  week  day_of_week  day_of_month  \\\n",
       "Date_Time                                                                      \n",
       "2004-04-04 00:00:00     1224.0  56.5      4    14            6             4   \n",
       "2004-04-04 01:00:00     1215.0  59.2      4    14            6             4   \n",
       "2004-04-04 02:00:00     1115.0  62.4      4    14            6             4   \n",
       "2004-04-04 03:00:00     1124.0  65.0      4    14            6             4   \n",
       "2004-04-04 04:00:00     1028.0  65.3      4    14            6             4   \n",
       "\n",
       "                     hour  weekend  \n",
       "Date_Time                           \n",
       "2004-04-04 00:00:00     0        1  \n",
       "2004-04-04 01:00:00     1        1  \n",
       "2004-04-04 02:00:00     2        1  \n",
       "2004-04-04 03:00:00     3        1  \n",
       "2004-04-04 04:00:00     4        1  "
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dtf = DatetimeFeatures(\n",
    "    # the datetime variable\n",
    "    variables=\"index\",\n",
    "    \n",
    "    # the features we want to create\n",
    "    features_to_extract=[\n",
    "        \"month\",\n",
    "        \"week\",\n",
    "        \"day_of_week\",\n",
    "        \"day_of_month\",\n",
    "        \"hour\",\n",
    "        \"weekend\",\n",
    "    ],\n",
    ")\n",
    "\n",
    "# Extract the datetime features\n",
    "data = dtf.fit_transform(data)\n",
    "\n",
    "# Show new variables\n",
    "data.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Lag features\n",
    "\n",
    "We create the following lagged features:\n",
    "\n",
    "- The pollutant concentration for the previous hour (t-1).\n",
    "\n",
    "- The pollutant concentration for the same hour on the previous day (t-24).\n",
    "\n",
    "[LagFeatures](https://feature-engine.readthedocs.io/en/latest/api_doc/timeseries/forecasting/LagFeatures.html)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>CO_sensor_lag_1H</th>\n",
       "      <th>RH_lag_1H</th>\n",
       "      <th>CO_sensor_lag_24H</th>\n",
       "      <th>RH_lag_24H</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Date_Time</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2004-04-04 00:00:00</th>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2004-04-04 01:00:00</th>\n",
       "      <td>1224.0</td>\n",
       "      <td>56.5</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2004-04-04 02:00:00</th>\n",
       "      <td>1215.0</td>\n",
       "      <td>59.2</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2004-04-04 03:00:00</th>\n",
       "      <td>1115.0</td>\n",
       "      <td>62.4</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2004-04-04 04:00:00</th>\n",
       "      <td>1124.0</td>\n",
       "      <td>65.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                     CO_sensor_lag_1H  RH_lag_1H  CO_sensor_lag_24H  \\\n",
       "Date_Time                                                             \n",
       "2004-04-04 00:00:00               NaN        NaN                NaN   \n",
       "2004-04-04 01:00:00            1224.0       56.5                NaN   \n",
       "2004-04-04 02:00:00            1215.0       59.2                NaN   \n",
       "2004-04-04 03:00:00            1115.0       62.4                NaN   \n",
       "2004-04-04 04:00:00            1124.0       65.0                NaN   \n",
       "\n",
       "                     RH_lag_24H  \n",
       "Date_Time                        \n",
       "2004-04-04 00:00:00         NaN  \n",
       "2004-04-04 01:00:00         NaN  \n",
       "2004-04-04 02:00:00         NaN  \n",
       "2004-04-04 03:00:00         NaN  \n",
       "2004-04-04 04:00:00         NaN  "
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Add the lag features.\n",
    "\n",
    "lagf = LagFeatures(\n",
    "    variables=[\"CO_sensor\", \"RH\"],  # the input variables\n",
    "    freq=[\"1H\", \"24H\"],  # move 1 hr and 24 hrs forward\n",
    "    missing_values=\"ignore\",\n",
    ")\n",
    "\n",
    "# Add the lag features.\n",
    "data = lagf.fit_transform(data)\n",
    "\n",
    "# Show new variables\n",
    "data[[v for v in data.columns if \"lag\" in v]].head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Window features\n",
    "\n",
    "We take the average of the previous 3 hours of the time series to predict the current hour.\n",
    "\n",
    "[WindowFeatures](https://feature-engine.readthedocs.io/en/latest/api_doc/timeseries/forecasting/WindowFeatures.html)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>CO_sensor_window_3H_mean</th>\n",
       "      <th>RH_window_3H_mean</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Date_Time</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2004-04-04 00:00:00</th>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2004-04-04 01:00:00</th>\n",
       "      <td>1224.000000</td>\n",
       "      <td>56.500000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2004-04-04 02:00:00</th>\n",
       "      <td>1219.500000</td>\n",
       "      <td>57.850000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2004-04-04 03:00:00</th>\n",
       "      <td>1184.666667</td>\n",
       "      <td>59.366667</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2004-04-04 04:00:00</th>\n",
       "      <td>1151.333333</td>\n",
       "      <td>62.200000</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                     CO_sensor_window_3H_mean  RH_window_3H_mean\n",
       "Date_Time                                                       \n",
       "2004-04-04 00:00:00                       NaN                NaN\n",
       "2004-04-04 01:00:00               1224.000000          56.500000\n",
       "2004-04-04 02:00:00               1219.500000          57.850000\n",
       "2004-04-04 03:00:00               1184.666667          59.366667\n",
       "2004-04-04 04:00:00               1151.333333          62.200000"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "winf = WindowFeatures(\n",
    "    variables=[\"CO_sensor\", \"RH\"],  # the input variables\n",
    "    window=\"3H\",  # average of 3 previous hours\n",
    "    freq=\"1H\",  # move 1 hr forward\n",
    "    missing_values=\"ignore\",\n",
    ")\n",
    "\n",
    "# Add the window features.\n",
    "data = winf.fit_transform(data)\n",
    "\n",
    "# Show new variables\n",
    "data[[v for v in data.columns if \"window\" in v]].head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Periodic features\n",
    "\n",
    "We transform the month and the hour with the sine and cosine to have a periodic representation of the features.\n",
    "\n",
    "We automate this procedure with Feature-engine's [CyclicalFeatures](https://feature-engine.readthedocs.io/en/latest/api_doc/creation/CyclicalFeatures.html)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>month</th>\n",
       "      <th>day_of_month</th>\n",
       "      <th>hour</th>\n",
       "      <th>month_sin</th>\n",
       "      <th>month_cos</th>\n",
       "      <th>hour_sin</th>\n",
       "      <th>hour_cos</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Date_Time</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2004-04-04 00:00:00</th>\n",
       "      <td>4</td>\n",
       "      <td>4</td>\n",
       "      <td>0</td>\n",
       "      <td>0.866025</td>\n",
       "      <td>-0.5</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>1.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2004-04-04 01:00:00</th>\n",
       "      <td>4</td>\n",
       "      <td>4</td>\n",
       "      <td>1</td>\n",
       "      <td>0.866025</td>\n",
       "      <td>-0.5</td>\n",
       "      <td>0.269797</td>\n",
       "      <td>0.962917</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2004-04-04 02:00:00</th>\n",
       "      <td>4</td>\n",
       "      <td>4</td>\n",
       "      <td>2</td>\n",
       "      <td>0.866025</td>\n",
       "      <td>-0.5</td>\n",
       "      <td>0.519584</td>\n",
       "      <td>0.854419</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2004-04-04 03:00:00</th>\n",
       "      <td>4</td>\n",
       "      <td>4</td>\n",
       "      <td>3</td>\n",
       "      <td>0.866025</td>\n",
       "      <td>-0.5</td>\n",
       "      <td>0.730836</td>\n",
       "      <td>0.682553</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2004-04-04 04:00:00</th>\n",
       "      <td>4</td>\n",
       "      <td>4</td>\n",
       "      <td>4</td>\n",
       "      <td>0.866025</td>\n",
       "      <td>-0.5</td>\n",
       "      <td>0.887885</td>\n",
       "      <td>0.460065</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                     month  day_of_month  hour  month_sin  month_cos  \\\n",
       "Date_Time                                                              \n",
       "2004-04-04 00:00:00      4             4     0   0.866025       -0.5   \n",
       "2004-04-04 01:00:00      4             4     1   0.866025       -0.5   \n",
       "2004-04-04 02:00:00      4             4     2   0.866025       -0.5   \n",
       "2004-04-04 03:00:00      4             4     3   0.866025       -0.5   \n",
       "2004-04-04 04:00:00      4             4     4   0.866025       -0.5   \n",
       "\n",
       "                     hour_sin  hour_cos  \n",
       "Date_Time                                \n",
       "2004-04-04 00:00:00  0.000000  1.000000  \n",
       "2004-04-04 01:00:00  0.269797  0.962917  \n",
       "2004-04-04 02:00:00  0.519584  0.854419  \n",
       "2004-04-04 03:00:00  0.730836  0.682553  \n",
       "2004-04-04 04:00:00  0.887885  0.460065  "
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Create features that capture the cyclical representation.\n",
    "\n",
    "cyclicf = CyclicalFeatures(\n",
    "    # The features we want to transform.\n",
    "    variables=[\"month\", \"hour\"],\n",
    "    # Whether to drop the original features.\n",
    "    drop_original=False,\n",
    ")\n",
    "\n",
    "data = cyclicf.fit_transform(data)\n",
    "\n",
    "data[[v for v in data.columns if \"month\" in v or \"hour\" in v]].head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We can see the newly created features at the end of the dataframe.\n",
    "\n",
    "## Missing data\n",
    "\n",
    "When creating lag and window features, we introduced some missing data.\n",
    "\n",
    "[DropMissingData](https://feature-engine.readthedocs.io/en/latest/api_doc/imputation/DropMissingData.html)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "CO_sensor                     0\n",
       "RH                            0\n",
       "month                         0\n",
       "week                          0\n",
       "day_of_week                   0\n",
       "day_of_month                  0\n",
       "hour                          0\n",
       "weekend                       0\n",
       "CO_sensor_lag_1H             27\n",
       "RH_lag_1H                    27\n",
       "CO_sensor_lag_24H           461\n",
       "RH_lag_24H                  461\n",
       "CO_sensor_window_3H_mean     27\n",
       "RH_window_3H_mean            27\n",
       "month_sin                     0\n",
       "month_cos                     0\n",
       "hour_sin                      0\n",
       "hour_cos                      0\n",
       "dtype: int64"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.isnull().sum()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(7393, 18)\n",
      "(6922, 18)\n"
     ]
    }
   ],
   "source": [
    "# We drop the observations with NA.\n",
    "\n",
    "print(data.shape)\n",
    "\n",
    "imputer = DropMissingData()\n",
    "\n",
    "data = imputer.fit_transform(data)\n",
    "\n",
    "print(data.shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>CO_sensor</th>\n",
       "      <th>RH</th>\n",
       "      <th>month</th>\n",
       "      <th>week</th>\n",
       "      <th>day_of_week</th>\n",
       "      <th>day_of_month</th>\n",
       "      <th>hour</th>\n",
       "      <th>weekend</th>\n",
       "      <th>CO_sensor_lag_1H</th>\n",
       "      <th>RH_lag_1H</th>\n",
       "      <th>CO_sensor_lag_24H</th>\n",
       "      <th>RH_lag_24H</th>\n",
       "      <th>CO_sensor_window_3H_mean</th>\n",
       "      <th>RH_window_3H_mean</th>\n",
       "      <th>month_sin</th>\n",
       "      <th>month_cos</th>\n",
       "      <th>hour_sin</th>\n",
       "      <th>hour_cos</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Date_Time</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2004-04-05 00:00:00</th>\n",
       "      <td>1065.0</td>\n",
       "      <td>65.8</td>\n",
       "      <td>4</td>\n",
       "      <td>15</td>\n",
       "      <td>0</td>\n",
       "      <td>5</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1188.0</td>\n",
       "      <td>60.8</td>\n",
       "      <td>1224.0</td>\n",
       "      <td>56.5</td>\n",
       "      <td>1165.666667</td>\n",
       "      <td>58.566667</td>\n",
       "      <td>0.866025</td>\n",
       "      <td>-0.5</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>1.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2004-04-05 01:00:00</th>\n",
       "      <td>999.0</td>\n",
       "      <td>79.2</td>\n",
       "      <td>4</td>\n",
       "      <td>15</td>\n",
       "      <td>0</td>\n",
       "      <td>5</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1065.0</td>\n",
       "      <td>65.8</td>\n",
       "      <td>1215.0</td>\n",
       "      <td>59.2</td>\n",
       "      <td>1149.666667</td>\n",
       "      <td>61.800000</td>\n",
       "      <td>0.866025</td>\n",
       "      <td>-0.5</td>\n",
       "      <td>0.269797</td>\n",
       "      <td>0.962917</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2004-04-05 02:00:00</th>\n",
       "      <td>911.0</td>\n",
       "      <td>80.0</td>\n",
       "      <td>4</td>\n",
       "      <td>15</td>\n",
       "      <td>0</td>\n",
       "      <td>5</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>999.0</td>\n",
       "      <td>79.2</td>\n",
       "      <td>1115.0</td>\n",
       "      <td>62.4</td>\n",
       "      <td>1084.000000</td>\n",
       "      <td>68.600000</td>\n",
       "      <td>0.866025</td>\n",
       "      <td>-0.5</td>\n",
       "      <td>0.519584</td>\n",
       "      <td>0.854419</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2004-04-05 03:00:00</th>\n",
       "      <td>873.0</td>\n",
       "      <td>81.0</td>\n",
       "      <td>4</td>\n",
       "      <td>15</td>\n",
       "      <td>0</td>\n",
       "      <td>5</td>\n",
       "      <td>3</td>\n",
       "      <td>0</td>\n",
       "      <td>911.0</td>\n",
       "      <td>80.0</td>\n",
       "      <td>1124.0</td>\n",
       "      <td>65.0</td>\n",
       "      <td>991.666667</td>\n",
       "      <td>75.000000</td>\n",
       "      <td>0.866025</td>\n",
       "      <td>-0.5</td>\n",
       "      <td>0.730836</td>\n",
       "      <td>0.682553</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2004-04-05 04:00:00</th>\n",
       "      <td>881.0</td>\n",
       "      <td>81.0</td>\n",
       "      <td>4</td>\n",
       "      <td>15</td>\n",
       "      <td>0</td>\n",
       "      <td>5</td>\n",
       "      <td>4</td>\n",
       "      <td>0</td>\n",
       "      <td>873.0</td>\n",
       "      <td>81.0</td>\n",
       "      <td>1028.0</td>\n",
       "      <td>65.3</td>\n",
       "      <td>927.666667</td>\n",
       "      <td>80.066667</td>\n",
       "      <td>0.866025</td>\n",
       "      <td>-0.5</td>\n",
       "      <td>0.887885</td>\n",
       "      <td>0.460065</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                     CO_sensor    RH  month  week  day_of_week  day_of_month  \\\n",
       "Date_Time                                                                      \n",
       "2004-04-05 00:00:00     1065.0  65.8      4    15            0             5   \n",
       "2004-04-05 01:00:00      999.0  79.2      4    15            0             5   \n",
       "2004-04-05 02:00:00      911.0  80.0      4    15            0             5   \n",
       "2004-04-05 03:00:00      873.0  81.0      4    15            0             5   \n",
       "2004-04-05 04:00:00      881.0  81.0      4    15            0             5   \n",
       "\n",
       "                     hour  weekend  CO_sensor_lag_1H  RH_lag_1H  \\\n",
       "Date_Time                                                         \n",
       "2004-04-05 00:00:00     0        0            1188.0       60.8   \n",
       "2004-04-05 01:00:00     1        0            1065.0       65.8   \n",
       "2004-04-05 02:00:00     2        0             999.0       79.2   \n",
       "2004-04-05 03:00:00     3        0             911.0       80.0   \n",
       "2004-04-05 04:00:00     4        0             873.0       81.0   \n",
       "\n",
       "                     CO_sensor_lag_24H  RH_lag_24H  CO_sensor_window_3H_mean  \\\n",
       "Date_Time                                                                      \n",
       "2004-04-05 00:00:00             1224.0        56.5               1165.666667   \n",
       "2004-04-05 01:00:00             1215.0        59.2               1149.666667   \n",
       "2004-04-05 02:00:00             1115.0        62.4               1084.000000   \n",
       "2004-04-05 03:00:00             1124.0        65.0                991.666667   \n",
       "2004-04-05 04:00:00             1028.0        65.3                927.666667   \n",
       "\n",
       "                     RH_window_3H_mean  month_sin  month_cos  hour_sin  \\\n",
       "Date_Time                                                                \n",
       "2004-04-05 00:00:00          58.566667   0.866025       -0.5  0.000000   \n",
       "2004-04-05 01:00:00          61.800000   0.866025       -0.5  0.269797   \n",
       "2004-04-05 02:00:00          68.600000   0.866025       -0.5  0.519584   \n",
       "2004-04-05 03:00:00          75.000000   0.866025       -0.5  0.730836   \n",
       "2004-04-05 04:00:00          80.066667   0.866025       -0.5  0.887885   \n",
       "\n",
       "                     hour_cos  \n",
       "Date_Time                      \n",
       "2004-04-05 00:00:00  1.000000  \n",
       "2004-04-05 01:00:00  0.962917  \n",
       "2004-04-05 02:00:00  0.854419  \n",
       "2004-04-05 03:00:00  0.682553  \n",
       "2004-04-05 04:00:00  0.460065  "
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Drop original time series\n",
    "\n",
    "To avoid look-ahead bias.\n",
    "\n",
    "[DropFeatures](https://feature-engine.readthedocs.io/en/latest/api_doc/selection/DropFeatures.html)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>month</th>\n",
       "      <th>week</th>\n",
       "      <th>day_of_week</th>\n",
       "      <th>day_of_month</th>\n",
       "      <th>hour</th>\n",
       "      <th>weekend</th>\n",
       "      <th>CO_sensor_lag_1H</th>\n",
       "      <th>RH_lag_1H</th>\n",
       "      <th>CO_sensor_lag_24H</th>\n",
       "      <th>RH_lag_24H</th>\n",
       "      <th>CO_sensor_window_3H_mean</th>\n",
       "      <th>RH_window_3H_mean</th>\n",
       "      <th>month_sin</th>\n",
       "      <th>month_cos</th>\n",
       "      <th>hour_sin</th>\n",
       "      <th>hour_cos</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Date_Time</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2004-04-05 00:00:00</th>\n",
       "      <td>4</td>\n",
       "      <td>15</td>\n",
       "      <td>0</td>\n",
       "      <td>5</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1188.0</td>\n",
       "      <td>60.8</td>\n",
       "      <td>1224.0</td>\n",
       "      <td>56.5</td>\n",
       "      <td>1165.666667</td>\n",
       "      <td>58.566667</td>\n",
       "      <td>0.866025</td>\n",
       "      <td>-0.5</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>1.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2004-04-05 01:00:00</th>\n",
       "      <td>4</td>\n",
       "      <td>15</td>\n",
       "      <td>0</td>\n",
       "      <td>5</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1065.0</td>\n",
       "      <td>65.8</td>\n",
       "      <td>1215.0</td>\n",
       "      <td>59.2</td>\n",
       "      <td>1149.666667</td>\n",
       "      <td>61.800000</td>\n",
       "      <td>0.866025</td>\n",
       "      <td>-0.5</td>\n",
       "      <td>0.269797</td>\n",
       "      <td>0.962917</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2004-04-05 02:00:00</th>\n",
       "      <td>4</td>\n",
       "      <td>15</td>\n",
       "      <td>0</td>\n",
       "      <td>5</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>999.0</td>\n",
       "      <td>79.2</td>\n",
       "      <td>1115.0</td>\n",
       "      <td>62.4</td>\n",
       "      <td>1084.000000</td>\n",
       "      <td>68.600000</td>\n",
       "      <td>0.866025</td>\n",
       "      <td>-0.5</td>\n",
       "      <td>0.519584</td>\n",
       "      <td>0.854419</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2004-04-05 03:00:00</th>\n",
       "      <td>4</td>\n",
       "      <td>15</td>\n",
       "      <td>0</td>\n",
       "      <td>5</td>\n",
       "      <td>3</td>\n",
       "      <td>0</td>\n",
       "      <td>911.0</td>\n",
       "      <td>80.0</td>\n",
       "      <td>1124.0</td>\n",
       "      <td>65.0</td>\n",
       "      <td>991.666667</td>\n",
       "      <td>75.000000</td>\n",
       "      <td>0.866025</td>\n",
       "      <td>-0.5</td>\n",
       "      <td>0.730836</td>\n",
       "      <td>0.682553</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2004-04-05 04:00:00</th>\n",
       "      <td>4</td>\n",
       "      <td>15</td>\n",
       "      <td>0</td>\n",
       "      <td>5</td>\n",
       "      <td>4</td>\n",
       "      <td>0</td>\n",
       "      <td>873.0</td>\n",
       "      <td>81.0</td>\n",
       "      <td>1028.0</td>\n",
       "      <td>65.3</td>\n",
       "      <td>927.666667</td>\n",
       "      <td>80.066667</td>\n",
       "      <td>0.866025</td>\n",
       "      <td>-0.5</td>\n",
       "      <td>0.887885</td>\n",
       "      <td>0.460065</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                     month  week  day_of_week  day_of_month  hour  weekend  \\\n",
       "Date_Time                                                                    \n",
       "2004-04-05 00:00:00      4    15            0             5     0        0   \n",
       "2004-04-05 01:00:00      4    15            0             5     1        0   \n",
       "2004-04-05 02:00:00      4    15            0             5     2        0   \n",
       "2004-04-05 03:00:00      4    15            0             5     3        0   \n",
       "2004-04-05 04:00:00      4    15            0             5     4        0   \n",
       "\n",
       "                     CO_sensor_lag_1H  RH_lag_1H  CO_sensor_lag_24H  \\\n",
       "Date_Time                                                             \n",
       "2004-04-05 00:00:00            1188.0       60.8             1224.0   \n",
       "2004-04-05 01:00:00            1065.0       65.8             1215.0   \n",
       "2004-04-05 02:00:00             999.0       79.2             1115.0   \n",
       "2004-04-05 03:00:00             911.0       80.0             1124.0   \n",
       "2004-04-05 04:00:00             873.0       81.0             1028.0   \n",
       "\n",
       "                     RH_lag_24H  CO_sensor_window_3H_mean  RH_window_3H_mean  \\\n",
       "Date_Time                                                                      \n",
       "2004-04-05 00:00:00        56.5               1165.666667          58.566667   \n",
       "2004-04-05 01:00:00        59.2               1149.666667          61.800000   \n",
       "2004-04-05 02:00:00        62.4               1084.000000          68.600000   \n",
       "2004-04-05 03:00:00        65.0                991.666667          75.000000   \n",
       "2004-04-05 04:00:00        65.3                927.666667          80.066667   \n",
       "\n",
       "                     month_sin  month_cos  hour_sin  hour_cos  \n",
       "Date_Time                                                      \n",
       "2004-04-05 00:00:00   0.866025       -0.5  0.000000  1.000000  \n",
       "2004-04-05 01:00:00   0.866025       -0.5  0.269797  0.962917  \n",
       "2004-04-05 02:00:00   0.866025       -0.5  0.519584  0.854419  \n",
       "2004-04-05 03:00:00   0.866025       -0.5  0.730836  0.682553  \n",
       "2004-04-05 04:00:00   0.866025       -0.5  0.887885  0.460065  "
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "drop_ts = DropFeatures(features_to_drop=[\"CO_sensor\", \"RH\"])\n",
    "\n",
    "data = drop_ts.fit_transform(data)\n",
    "\n",
    "data.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Pipeline\n",
    "\n",
    "We have now created a lot of features that we can use to predict the CO concentration. Let's extract all these features in one step using a feature engineering pipeline."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>CO_sensor</th>\n",
       "      <th>RH</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Date_Time</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2004-04-04 00:00:00</th>\n",
       "      <td>1224.0</td>\n",
       "      <td>56.5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2004-04-04 01:00:00</th>\n",
       "      <td>1215.0</td>\n",
       "      <td>59.2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2004-04-04 02:00:00</th>\n",
       "      <td>1115.0</td>\n",
       "      <td>62.4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2004-04-04 03:00:00</th>\n",
       "      <td>1124.0</td>\n",
       "      <td>65.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2004-04-04 04:00:00</th>\n",
       "      <td>1028.0</td>\n",
       "      <td>65.3</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                     CO_sensor    RH\n",
       "Date_Time                           \n",
       "2004-04-04 00:00:00     1224.0  56.5\n",
       "2004-04-04 01:00:00     1215.0  59.2\n",
       "2004-04-04 02:00:00     1115.0  62.4\n",
       "2004-04-04 03:00:00     1124.0  65.0\n",
       "2004-04-04 04:00:00     1028.0  65.3"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Let's re-load the data, to start\n",
    "# from scratch.\n",
    "\n",
    "data = load_data()\n",
    "\n",
    "data.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "# We line up the engineering steps within\n",
    "# a pipeline.\n",
    "\n",
    "pipe = Pipeline(\n",
    "    [\n",
    "        (\"datetime_features\", dtf),\n",
    "        (\"lagf\", lagf),\n",
    "        (\"winf\", winf),\n",
    "        (\"Periodic\", cyclicf),\n",
    "        (\"dropna\", imputer),\n",
    "        (\"drop_ts\", drop_ts),\n",
    "    ]\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>month</th>\n",
       "      <th>week</th>\n",
       "      <th>day_of_week</th>\n",
       "      <th>day_of_month</th>\n",
       "      <th>hour</th>\n",
       "      <th>weekend</th>\n",
       "      <th>CO_sensor_lag_1H</th>\n",
       "      <th>RH_lag_1H</th>\n",
       "      <th>CO_sensor_lag_24H</th>\n",
       "      <th>RH_lag_24H</th>\n",
       "      <th>CO_sensor_window_3H_mean</th>\n",
       "      <th>RH_window_3H_mean</th>\n",
       "      <th>month_sin</th>\n",
       "      <th>month_cos</th>\n",
       "      <th>hour_sin</th>\n",
       "      <th>hour_cos</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Date_Time</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2004-04-05 00:00:00</th>\n",
       "      <td>4</td>\n",
       "      <td>15</td>\n",
       "      <td>0</td>\n",
       "      <td>5</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1188.0</td>\n",
       "      <td>60.8</td>\n",
       "      <td>1224.0</td>\n",
       "      <td>56.5</td>\n",
       "      <td>1165.666667</td>\n",
       "      <td>58.566667</td>\n",
       "      <td>0.866025</td>\n",
       "      <td>-0.5</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>1.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2004-04-05 01:00:00</th>\n",
       "      <td>4</td>\n",
       "      <td>15</td>\n",
       "      <td>0</td>\n",
       "      <td>5</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1065.0</td>\n",
       "      <td>65.8</td>\n",
       "      <td>1215.0</td>\n",
       "      <td>59.2</td>\n",
       "      <td>1149.666667</td>\n",
       "      <td>61.800000</td>\n",
       "      <td>0.866025</td>\n",
       "      <td>-0.5</td>\n",
       "      <td>0.269797</td>\n",
       "      <td>0.962917</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2004-04-05 02:00:00</th>\n",
       "      <td>4</td>\n",
       "      <td>15</td>\n",
       "      <td>0</td>\n",
       "      <td>5</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>999.0</td>\n",
       "      <td>79.2</td>\n",
       "      <td>1115.0</td>\n",
       "      <td>62.4</td>\n",
       "      <td>1084.000000</td>\n",
       "      <td>68.600000</td>\n",
       "      <td>0.866025</td>\n",
       "      <td>-0.5</td>\n",
       "      <td>0.519584</td>\n",
       "      <td>0.854419</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2004-04-05 03:00:00</th>\n",
       "      <td>4</td>\n",
       "      <td>15</td>\n",
       "      <td>0</td>\n",
       "      <td>5</td>\n",
       "      <td>3</td>\n",
       "      <td>0</td>\n",
       "      <td>911.0</td>\n",
       "      <td>80.0</td>\n",
       "      <td>1124.0</td>\n",
       "      <td>65.0</td>\n",
       "      <td>991.666667</td>\n",
       "      <td>75.000000</td>\n",
       "      <td>0.866025</td>\n",
       "      <td>-0.5</td>\n",
       "      <td>0.730836</td>\n",
       "      <td>0.682553</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2004-04-05 04:00:00</th>\n",
       "      <td>4</td>\n",
       "      <td>15</td>\n",
       "      <td>0</td>\n",
       "      <td>5</td>\n",
       "      <td>4</td>\n",
       "      <td>0</td>\n",
       "      <td>873.0</td>\n",
       "      <td>81.0</td>\n",
       "      <td>1028.0</td>\n",
       "      <td>65.3</td>\n",
       "      <td>927.666667</td>\n",
       "      <td>80.066667</td>\n",
       "      <td>0.866025</td>\n",
       "      <td>-0.5</td>\n",
       "      <td>0.887885</td>\n",
       "      <td>0.460065</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                     month  week  day_of_week  day_of_month  hour  weekend  \\\n",
       "Date_Time                                                                    \n",
       "2004-04-05 00:00:00      4    15            0             5     0        0   \n",
       "2004-04-05 01:00:00      4    15            0             5     1        0   \n",
       "2004-04-05 02:00:00      4    15            0             5     2        0   \n",
       "2004-04-05 03:00:00      4    15            0             5     3        0   \n",
       "2004-04-05 04:00:00      4    15            0             5     4        0   \n",
       "\n",
       "                     CO_sensor_lag_1H  RH_lag_1H  CO_sensor_lag_24H  \\\n",
       "Date_Time                                                             \n",
       "2004-04-05 00:00:00            1188.0       60.8             1224.0   \n",
       "2004-04-05 01:00:00            1065.0       65.8             1215.0   \n",
       "2004-04-05 02:00:00             999.0       79.2             1115.0   \n",
       "2004-04-05 03:00:00             911.0       80.0             1124.0   \n",
       "2004-04-05 04:00:00             873.0       81.0             1028.0   \n",
       "\n",
       "                     RH_lag_24H  CO_sensor_window_3H_mean  RH_window_3H_mean  \\\n",
       "Date_Time                                                                      \n",
       "2004-04-05 00:00:00        56.5               1165.666667          58.566667   \n",
       "2004-04-05 01:00:00        59.2               1149.666667          61.800000   \n",
       "2004-04-05 02:00:00        62.4               1084.000000          68.600000   \n",
       "2004-04-05 03:00:00        65.0                991.666667          75.000000   \n",
       "2004-04-05 04:00:00        65.3                927.666667          80.066667   \n",
       "\n",
       "                     month_sin  month_cos  hour_sin  hour_cos  \n",
       "Date_Time                                                      \n",
       "2004-04-05 00:00:00   0.866025       -0.5  0.000000  1.000000  \n",
       "2004-04-05 01:00:00   0.866025       -0.5  0.269797  0.962917  \n",
       "2004-04-05 02:00:00   0.866025       -0.5  0.519584  0.854419  \n",
       "2004-04-05 03:00:00   0.866025       -0.5  0.730836  0.682553  \n",
       "2004-04-05 04:00:00   0.866025       -0.5  0.887885  0.460065  "
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Fit the pipeline to the data and add\n",
    "# features.\n",
    "\n",
    "data = pipe.fit_transform(data)\n",
    "\n",
    "data.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "In the next notebook, we will train a linear regression at the back of this feature engineering pipeline to forecast 1 step ahead. \n",
    "\n",
    "That is all for this notebook. I hope you enjoyed it!"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "fsml",
   "language": "python",
   "name": "fsml"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.5"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {
    "height": "calc(100% - 180px)",
    "left": "10px",
    "top": "150px",
    "width": "211.6px"
   },
   "toc_section_display": true,
   "toc_window_display": true
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
